﻿#region Copyright
//————————————————————————————————————————//
//	创建人员(Creator)    ：Huangyd
//	创建日期(Date)       ：2012-07-03
//  联系方式(Email)      ：aj-ayumi@163.com; gajayumi@gmail.com; ajayumi@me.com
//  描    述(Description)：
//
//	Copyright (C) 2012 ajayumi 版权所有
//————————————————————————————————————————//
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ajayumi.Platform.Search.Properties;

namespace ajayumi.Platform.Search.Win
{
    /// <summary>
    /// SearchPanel
    /// </summary>
    [ToolboxItem(true)]
    public partial class SearchPanel : UserControl
    {
        #region Variables

        private SearchPanelMode m_Mode = SearchPanelMode.Common;
        private const string COMMON_SEARCH_NAME = "CommonSearchID";

        #endregion

        #region Properties

        /// <summary>
        /// 查询模式，普通、高级
        /// </summary>
        [Browsable(true)]
        [Description("查询模式")]
        [Category("ajayumi Properties")]
        public SearchPanelMode Mode
        {
            get { return m_Mode; }
            set
            {
                m_Mode = value;
                this.InitSearchComponent();
            }
        }

        [Browsable(false)]
        public SearchBase Searcher { get; set; }

        #endregion

        //protected override void OnPaint(PaintEventArgs e)
        //{
        //    base.OnPaint(e);
        //    Graphics g = e.Graphics;
        //    g.DrawRectangle(new Pen(Color.Red, 2f), e.ClipRectangle);
        //    this.Invalidate();
        //}

        public SearchPanel()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.InitSearchComponent();
        }

        /// <summary>
        /// 初始化查询组件
        /// </summary>
        private void InitSearchComponent()
        {
            if (DesignMode || this.Searcher == null) return;

            this.tlPanel.Controls.Clear();
            this.tlPanel.ColumnCount = 1;
            this.tlPanel.RowCount = 1;
            this.tlPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));

            switch (this.Mode)
            {
                case SearchPanelMode.Common:
                    SearchTextBox stb = new SearchTextBox()
                    {
                        Name = COMMON_SEARCH_NAME,
                        Dock = DockStyle.Fill,
                        DisplayText = Resources.sTxt_DisplayName
                    };
                    this.tlPanel.Controls.Add(stb, 0, 0);
                    break;
                case SearchPanelMode.Expert:
                    CreateSearchControls();
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 创建查询控件，用于高级查询的时候，自动生成子控件
        /// </summary>
        private void CreateSearchControls()
        {
            List<SearchItem> lst = this.Searcher.CreateItems();
            int count = lst.Count;
            SearchControlBase control = null;

            this.tlPanel.ColumnCount = 2;
            this.tlPanel.RowCount = count / 2;
            this.tlPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));


            int col = 0, row = 0;
            for (int i = 0; i < count; i++)
            {
                SearchItem item = lst[i];

                switch (item.ControlType)
                {
                    case ValueControlType.TextBox:
                        control = new SearchTextBox();
                        break;
                    case ValueControlType.DateTimePicker:
                        control = new SearchDateTimePicker();
                        break;
                    case ValueControlType.DropdownList:
                        control = new SearchDropdownList(item.SearchOptions);
                        break;
                    case ValueControlType.RangeTextBox:
                        control = new SearchRangeTextBox();
                        break;
                    case ValueControlType.RangeDateTimePicker:
                        control = new SearchRangeDateTimePicker();
                        break;
                    case ValueControlType.RangeDropdownList:
                        control = new SearchRangeDropdownList(item.SearchOptions, item.SearchOptions2);
                        break;
                    default:
                        break;
                }
                control.Dock = DockStyle.Fill;
                control.ItemIndex = i;
                control.DisplayText = item.DisplayName;
                control.ValueType = item.ValueType;

                this.tlPanel.Controls.Add(control, col++, row);

                if (col > 1)
                {
                    col = 0;
                    row++;
                }
            }
        }


        /// <summary>
        /// 获取拼接的sql字符串
        /// </summary>
        /// <returns></returns>
        public string GetSql()
        {
            List<SearchItemValue> lst = new List<SearchItemValue>();
            foreach (Control item in this.tlPanel.Controls)
            {
                if (item is SearchControlBase)
                {
                    SearchItemValue siv = ((SearchControlBase)item).GetSearchItemValue();
                    lst.Add(siv);
                }
            }

            switch (this.m_Mode)
            {
                case SearchPanelMode.Common:
                    string str = string.Empty;
                    if (lst.Count > 0)
                    { str = lst[0].Value1; }
                    return this.Searcher.BuildQuickSearchSql(str);
                case SearchPanelMode.Expert:
                    return this.Searcher.BuildSearchSql(lst);
            }
            return string.Empty;
        }


        //void Control_KeyDown(object sender, KeyEventArgs e)
        //{
        //    if (e.KeyCode == Keys.Enter)
        //    {
        //        this.OnSearchControlEnterDown(e);
        //    }
        //}

        ///// <summary>
        ///// 查询控件按下回车键事件
        ///// </summary>
        //[Description("查询控件按下回车键事件")]
        //[Category("ajayumi Events")]
        //public event EventHandler SearchControlEnterDown;
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="e"></param>
        //protected virtual void OnSearchControlEnterDown(EventArgs e)
        //{
        //    if (null != this.SearchControlEnterDown)
        //    {
        //        this.SearchControlEnterDown(this, e);
        //    }
        //}

    }
}
